package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.ParamSettingDTO;
import com.ideal.manage.esms.bean.project.SysBrandMakerDTO;
import com.ideal.manage.esms.bean.project.brand.BrandBO;
import com.ideal.manage.esms.bean.project.brand.CarModelBO;
import com.ideal.manage.esms.bean.project.brand.TreeAlphaBO;
import com.ideal.manage.esms.repository.programme.ParamSettingManageRepository;
import com.ideal.manage.esms.repository.programme.SysBrandMakerRepository;
import com.ideal.manage.esms.util.PinYinUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by 83730 on 2018/4/24.
 */
@Service
public class ParamSettingManageService {

    @Autowired
    private ParamSettingManageRepository paramSettingManageRepository;
    @Autowired
    private SysBrandMakerRepository sysBrandMakerRepository;

    public Map<String, List<ParamSettingDTO>> findAllParamSetting() {
        List<ParamSettingDTO> list = paramSettingManageRepository.findAll();
        Map<String, List<ParamSettingDTO>> maps = new HashMap<>();
        for(ParamSettingDTO paramSettingDTO : list){
            List<ParamSettingDTO> pList = maps.get(paramSettingDTO.getType());
            if(pList==null){
                pList= new LinkedList<>();
                maps.put(paramSettingDTO.getType(), pList);
            }
            pList.add(paramSettingDTO);
        }
        return maps;
    }


    public void updateParamSettingValue(String id, String value, String paramKey) {
        ParamSettingDTO dto = paramSettingManageRepository.findById(id);
        dto.setValue(value);
        if(StringUtils.isNotBlank(paramKey)){
            dto.setKey(paramKey);
        }
        paramSettingManageRepository.save(dto);
    }

    @Cacheable(value = "getDisplayBrandList")
    public Map<String, TreeAlphaBO> getDisplayBrandList(){
        List<SysBrandMakerDTO> all = sysBrandMakerRepository.findAll();
        //所有字母, key=字母, value=品牌
        Map<String, TreeAlphaBO> treeAlphaBOMap = new HashMap<>();
        for(SysBrandMakerDTO dto: all){

            ///////////talpha:字母, brandBOS:品牌
            TreeAlphaBO treeAlphaBO = treeAlphaBOMap.get(dto.getAlpha());
            if(treeAlphaBO==null){
                treeAlphaBO = new TreeAlphaBO();
                treeAlphaBO.setAlpha(dto.getAlpha());
                treeAlphaBOMap.put(dto.getAlpha(), treeAlphaBO);
            }
            //key=品牌, value=车型
            Map<String, BrandBO> brandMap = treeAlphaBO.getBrandBOS();
            if(brandMap==null){
                brandMap = new HashMap<>();
                treeAlphaBO.setBrandBOS(brandMap);
            }
            //车型层
            BrandBO brandBO = brandMap.get(dto.getBrand());
            if(brandBO==null){
                brandBO = new BrandBO();
                brandBO.setBrandName(dto.getBrand());
                brandMap.put(dto.getBrand(), brandBO);
            }
            List<CarModelBO> carModels = brandBO.getCarModels();
            if(carModels==null){
                carModels=new LinkedList<>();
                brandBO.setCarModels(carModels);
            }
            CarModelBO carModelBO = new CarModelBO();
            carModels.add(carModelBO);
            //最终车型
            carModelBO.setModelName(dto.getMaker());
        }
        return treeAlphaBOMap;
    }
    public void importSysBrandMaker(List<SysBrandMakerDTO> list){
        if(CollectionUtils.isEmpty(list)){
            return ;
        }
        sysBrandMakerRepository.deleteAll();
//        sysBrandMakerRepository.save(list.iterator())
        for(SysBrandMakerDTO dto: list) {
            dto.setNewtime(new Date());
            char firstChar = dto.getBrand().charAt(0);
            char alphaChar[]=null;
            if((firstChar>='A' && firstChar <='Z') || (firstChar>='a' && firstChar<='z')){
                char [] c = {firstChar};
                alphaChar=c;
            }else{
                try {
                    char[] c = {PinYinUtil.getCharPinYinChar(dto.getBrand().charAt(0))[0]};
                    alphaChar = c;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            dto.setAlpha(new String(alphaChar).toUpperCase());
            if(dto.getAlpha().length()>1) {
                System.out.println("============" + dto.getAlpha());
            }
            sysBrandMakerRepository.save(dto);
        }
    }
}
